import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:yuellyfactory/util/AuthUtil.dart';

import '../../MainPageVC.dart';
import '../../util/BaseRequest.dart';
import '../../util/StorageUtil.dart';
import '../../util/ToastUtil.dart';
import '../../util/ViewUtil.dart';


class LoginVC extends StatefulWidget {
  const LoginVC({super.key});

  @override
  State<LoginVC> createState() => _LoginVCState();
}

class _LoginVCState extends State<LoginVC> {

  TextEditingController accountController = TextEditingController();
  TextEditingController pwdController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return

      Scaffold(
        extendBodyBehindAppBar: true,
        body: Stack(
          children: [
            ViewUtil.defalutWhiteBg(context),
            ViewUtil.defaultTopBg(context),
            //中间的数据
            Positioned(
              //状态栏+appbar高度
              top: ScreenUtil().statusBarHeight + kToolbarHeight,
              bottom: ViewUtil.screenHeight(185),
              left: ViewUtil.screenWidth(300),
              right: ViewUtil.screenWidth(300),
              child:
              SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SizedBox(height: ViewUtil.screenHeight(100),),
                    Image.asset(
                      'images/logo.png', width: ViewUtil.screenWidth(200),),
                    SizedBox(height: ViewUtil.screenHeight(100),),
                    Container(
                      height: ViewUtil.screenHeight(100),
                      width: ViewUtil.matchWidth(context) -
                          ViewUtil.screenWidth(180),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.only(left: ViewUtil.screenWidth(60)),
                      decoration: BoxDecoration(
                        color: const Color(0xffF4F6FA),
                        borderRadius: BorderRadius.circular(ViewUtil.radio(12)),
                      ),
                      child:
                      Row(

                        children: [

                          Image.asset('images/login_account.png',
                            width: ViewUtil.screenWidth(50),),
                          SizedBox(width: ViewUtil.screenWidth(80),),
                          Expanded(child: TextField(
                            controller: accountController,
                            maxLines: 1,
                            style: TextStyle(color: const Color(0xFF303C52),
                                fontSize: ViewUtil.fontSize(32)),
                            decoration: InputDecoration(
                              //垂直居中
                              isCollapsed: true,
                              hintText: '登录账号',
                              border: InputBorder.none,
                              hintStyle: const TextStyle(
                                  color: Color(0xFFB7B7B7)),
                            ),
                          ),),

                        ],
                      ),


                    ),
                    SizedBox(height: ViewUtil.screenHeight(40),),
                    Container(
                      height: ViewUtil.screenHeight(100),
                      width: ViewUtil.matchWidth(context) -
                          ViewUtil.screenWidth(180),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.only(left: ViewUtil.screenWidth(60)),
                      decoration: BoxDecoration(
                        color: const Color(0xffF4F6FA),
                        borderRadius: BorderRadius.circular(ViewUtil.radio(12)),
                      ),
                      child:
                      Row(

                        children: [

                          Image.asset('images/login_pwd.png',
                            width: ViewUtil.screenWidth(50),),
                          SizedBox(width: ViewUtil.screenWidth(80),),
                          Expanded(child: TextField(
                            controller: pwdController,
                            maxLines: 1,
                            obscureText: true,
                            style: TextStyle(color: const Color(0xFF303C52),
                                fontSize: ViewUtil.fontSize(32)),
                            decoration: InputDecoration(
                              //垂直居中
                              isCollapsed: true,
                              hintText: '请输入密码',
                              border: InputBorder.none,
                              hintStyle: const TextStyle(
                                  color: Color(0xFFB7B7B7)),
                            ),
                          ),),

                        ],
                      ),


                    ),

                    SizedBox(height: ViewUtil.screenHeight(120),),
                    ViewUtil.textButton(
                        '登录',
                        ViewUtil.screenWidth(600),
                        ViewUtil.screenHeight(40),
                        ViewUtil.radio(12),
                        const Color(0xfff4f4f4),
                        const Color(0xfff4f4f4),
                        const Color(0xff72747A),
                        ViewUtil.fontSize(32), () {
                      toLogin();
                    })
                  ],
                ),
              ),
            ),

          ],

        ),
      );
  }

  void toLogin() {
    var account = accountController.text;
    var pwd = pwdController.text;
    if (account.isEmpty) {
      ToastUtil.showToast('请输入正确账号');
      return;
    }
    if (pwd.isEmpty) {
      ToastUtil.showToast('请输入密码');
      return;
    }
    BaseRequest.postHttp("user/login",
        parameters: {"username": account, "password": pwd}
        ,
        canTip: true,
        context: context,
        onSuccess: (data) async {
          bool result = BaseRequest.chekResponse(data, true);
          if (result) {
            var v = data["data"];
            var token = v["token"];
            var userId = v["userId"];
            var username = v["username"];
            var roleName = v["roleName"];
            var roleCode = v["roleCode"];
            var disCount = v["discount"];
            var menusApp=v["menusApp"];
            var buttonsApp=v["buttonsApp"];
            StorageUtil.saveDiscount(disCount.toString());
            StorageUtil.saveUserName(username);
            StorageUtil.saveToken(token);
            StorageUtil.saveLoginAccount(account);
            StorageUtil.saveRoleName(roleName);
            StorageUtil.saveUserId(userId);
            StorageUtil.saveRoleCode(roleCode);

            StorageUtil.saveMenu(menusApp);
            StorageUtil.saveMenuButton(buttonsApp);

            AuthUtil.instance.initDatas();

            ToastUtil.showToast('登录成功');



            Get.off(const MainPageVC());
          }
        },
        onError: (error) {});
  }

}